// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#include <sys/stat.h>
#include <fcntl.h>
#include "lc_validate_op_Operator.h"
#define LAGNIAPE_ROOT_DIR "/home/riche/"
#define BUFFER_SIZE 8192


void 
validate_op::validateRequest(requestTypes::NetRequest * data, unsigned long dataSize)
{
  assert(data != NULL);
  assert(data->getSocketID()>= 0);
  requestTypes::NetRequest *out_data;
  char *buffer=NULL; 
  char *buf=strdup(data->getBuffer());
  int length = 0;
  bool doneRequest = false;
  bool close = false;
  int numBytes = data->getNumBytes();
  
  do {
    char *start = buf;
    char *end;
    for (int i=0;i< numBytes;i++) {
      if (buf[i] == '\r') {
 	buf[i++] = 0;
 	buf[i] = 0; // Get rid of the \n
	//DEBUG printf("%s\n", start);
	if (length == 0) { // We're done...
	  doneRequest = true;
	  break;
	}
	else if(start[0]=='G' && start[1]=='E' && start[2]=='T'){
	  start = strchr(start, ' ')+1;
	  end = strchr(start+1, ' ');
	  *end = 0;
	  while (*end != 'H')
	    end++;
	  while (*end != '/')
	    end++;
	  end++;
	  int major = *end-'0'; // HACK HACK HACK Assumes ASCII
	  end+=2;
	  int minor = *end-'0'; // HACK HACK HACK Assumes ASCII
	  if (major != 1 || (minor > 1))
	    printf("Urm, HTTP version: %d.%d we may be in trouble...\n",
		   major, minor);
	  if (major == 1 && minor == 0) {
	    close = true;
	  }
	  
	  if (*start == '/')
	    start++;
	  
	  buffer = strdup(start);
	}
	else if (start[0]=='C' && start[1]=='o' && start[2]=='n'&&
		 strstr(start, "close")) {
	  close = true;
	}
	start=buf+i+1;
	length = 0;
      }
      else {
	length++;
      }
    }
  } while (!doneRequest);
  
  std::string filename;
  
  if (buffer == NULL) {
    filename = "/sys_error.html";
  }
  else {
    for(unsigned int i = 0; i < strlen(buffer); ++i) {
      filename += *(buffer + i);
    }
  }
  
  free(buf);
  free(buffer);
  
  out_data = new requestTypes::NetRequest(NULL,0, data->getSocketID(), filename);
  if(close) out_data->setClose(close);
  assert(out_data->getSocketID()>= 0);
  
  if(out_data != NULL) {
    if(filename.length() < 20) {
      short_requests(out_data, sizeof(requestTypes::NetRequest));
    }
    else {
      long_requests(out_data, sizeof(requestTypes::NetRequest));
    }
  }
  
  // the following clause is a hack
  if(!inProf) {
    //delete[] data->getBuffer();
    delete data;
  }
  
  return;
  
}

requestTypes::NetRequest *
validate_op::geninRequest()
{
  std::string request = "GET /index.html HTTP/1.1";
  request += "\r\n\r\n";
  char * request_str = new char[request.length()];
  strcpy(request_str, request.c_str());
  //std::cerr << request_str << ":" << strlen(request_str) << ":" << request.length() << std::endl;
  return new requestTypes::NetRequest(request_str,strlen(request_str),0);

}


